package cn.tiga.basic.controller;

import cn.tiga.basic.dto.SmsCodeDto;
import cn.tiga.basic.exception.BusinessException;
import cn.tiga.basic.service.IVerifyCodeService;
import cn.tiga.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.rmi.registry.Registry;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

   /** @Title: graph
   * @Description: 获取图形验证码
   * @Author: tiga
   * @Version: 1.0
   * @Date:  2022/7/6 15:31
   * @Parameters: [java.lang.String]
   * @Return cn.tiga.basic.utils.AjaxResult
   */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String imageBase64Str = verifyCodeService.getImageCode(imageCodeKey);
            return AjaxResult.me().setResultObj(imageBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeService.smsCode(smsCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
